package com.icen.utils.comm;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import android.util.Base64;

/**
 * Encryption Utils Class
 * 
 * @author IcenHan
 * 
 */
public class CipherUtils {
	private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	/**
	 * 获取MD5加密值
	 * 
	 * @param enStr
	 * @return String
	 */
	public static String getMd5Encode(String enStr) {
		byte[] inStrBytes = enStr.getBytes();
		try {
			MessageDigest MD = MessageDigest.getInstance("MD5");
			MD.update(inStrBytes);
			byte[] mdByte = MD.digest();
			char[] str = new char[mdByte.length * 2];
			int k = 0;
			for (int i = 0; i < mdByte.length; i++) {
				byte temp = mdByte[i];
				str[k++] = hexDigits[temp >>> 4 & 0xf];
				str[k++] = hexDigits[temp & 0xf];
			}
			return new String(str);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取Gzip加密值
	 * 
	 * @param enStr
	 * @return String
	 */
	public static String getGzipEncode(String enStr) {
		if (ValidateUtils.isEmpty(enStr)) {
			return null;
		}
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			GZIPOutputStream gzip = new GZIPOutputStream(baos);
			gzip.write(enStr.getBytes("UTF-8"));
			gzip.close();
			byte[] encode = baos.toByteArray();
			baos.flush();
			baos.close();
			return new String(encode, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取Gzip解密值
	 * 
	 * @param deStr
	 * @return String
	 */
	public static String getGzipDecode(String deStr) {
		if (ValidateUtils.isEmpty(deStr)) {
			return null;
		}
		try {
			byte[] decode = deStr.getBytes("UTF-8");
			ByteArrayInputStream bais = new ByteArrayInputStream(decode);
			GZIPInputStream gzip = new GZIPInputStream(bais);
			byte[] buf = new byte[1024];
			int len = 0;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((len = gzip.read(buf, 0, 1024)) != -1) {
				baos.write(buf, 0, len);
			}
			gzip.close();
			baos.flush();
			decode = baos.toByteArray();
			baos.close();
			return new String(decode, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取Base64加密值
	 * 
	 * @param enStr
	 * @return String
	 */
	public static String getBase64Encode(String enStr) {
		if (ValidateUtils.isEmpty(enStr)) {
			return null;
		}
		try {
			byte[] encode = enStr.getBytes("UTF-8");
			return new String(Base64.encode(encode, 0, encode.length,
					Base64.DEFAULT), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取Base64解密值
	 * 
	 * @param deStr
	 * @return String
	 */
	public static String getBase64Decode(String deStr) {
		if (ValidateUtils.isEmpty(deStr)) {
			return null;
		}
		try {
			byte[] encode = deStr.getBytes("UTF-8");
			return new String(Base64.decode(encode, 0, encode.length,
					Base64.DEFAULT), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取Utf-8加密值
	 * @param enStr
	 * @return String
	 */
	public static String getUtf8Encode(String enStr){
		if (!ValidateUtils.isEmpty(enStr) && enStr.getBytes().length != enStr.length()) {
			try {
				return URLEncoder.encode(enStr, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException(
						"UnsupportedEncodingException occurred. ", e);
			}
		}
		return null;
	}
}
